package com.example.mall.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.mall.constants.BaseConstants;
import com.example.mall.domain.CouponUsage;
import com.example.mall.dto.CouponUsageDto;
import com.example.mall.mapper.CouponUsageMapper;
import com.example.mall.query.CouponUsageQuery;
import com.example.mall.query.PageDTO;
import com.example.mall.service.ICouponUsageService;
import com.example.mall.util.AssertUtil;
import com.example.mall.util.CodeGenerateUtils;
import com.example.mall.util.CopyUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * <p>
 * 优惠券使用记录表 服务实现类
 * </p>
 *
 * @author whale
 * @since 2025-05-06
 */
@Service
public class CouponUsageServiceImpl extends ServiceImpl<CouponUsageMapper, CouponUsage> implements ICouponUsageService {

    @Autowired
    private CouponUsageMapper couponUsageMapper;

    @Override
    public PageDTO<CouponUsageDto> queryPage(CouponUsageQuery couponUsageQuery) {
        if (couponUsageQuery == null) {
            couponUsageQuery = new CouponUsageQuery();
        }
        Page<CouponUsage> page = couponUsageQuery.toMpPageByDefaultCreateTimeAsc();
        Page<CouponUsage> cartPage = lambdaQuery()
                .like(couponUsageQuery.getUsageId()!= null, CouponUsage::getUsageId, couponUsageQuery.getUsageId())
                .like(couponUsageQuery.getUserCouponId()!= null, CouponUsage::getUserCouponId, couponUsageQuery.getUserCouponId())
                .like(couponUsageQuery.getOrderId()!= null, CouponUsage::getOrderId, couponUsageQuery.getOrderId())
                .like(couponUsageQuery.getDiscountAmount()!= null, CouponUsage::getDiscountAmount, couponUsageQuery.getDiscountAmount())
                .like(couponUsageQuery.getStatus()!= null, CouponUsage::getStatus, couponUsageQuery.getStatus())
                .apply(couponUsageQuery.getUseTime() != null,
                        "DATE_FORMAT(use_time, '%Y-%m-%d') = DATE_FORMAT({0}, '%Y-%m-%d')",
                        couponUsageQuery.getUseTime())
                .orderByDesc(CouponUsage::getUpdateDate)
                .page(page);
        return PageDTO.of(cartPage, CouponUsageDto.class);
    }

    @Override
    public CouponUsage queryCouponUsageById(Long usageId) {
        AssertUtil.isNotNull(usageId, "优惠券使用记录ID不能为空");
        return couponUsageMapper.selectById(usageId);
    }

    @Override
    public List<CouponUsage> queryCouponUsageList() {
        return this.list();
    }

    @Override
    public void insert(CouponUsageDto couponUsageDto) {
        CouponUsage couponUsage = new CouponUsage();
        CopyUtils.copyProperties(couponUsageDto, couponUsage);
        couponUsage.setUsageId(CodeGenerateUtils.generateUniqueId());
        couponUsage.setCreateDate(new Date());
        couponUsage.setUpdateDate(new Date());
        couponUsage.setStatus(BaseConstants.EFFECTIVE_STATUS);
        couponUsageMapper.insert(couponUsage);
    }

    @Override
    public void update(CouponUsageDto couponUsageDto) {
        AssertUtil.isNotNull(couponUsageDto.getUsageId(), "优惠券使用记录ID不能为空");
        CouponUsage couponUsage = couponUsageMapper.selectById(couponUsageDto.getUsageId());
        AssertUtil.isNotNull(couponUsage, "优惠券使用记录不存在");
        CopyUtils.copyProperties(couponUsageDto, couponUsage);
        couponUsage.setUpdateDate(new Date());
        couponUsageMapper.updateById(couponUsage);
    }

    @Override
    public void delete(Long usageId) {
        AssertUtil.isNotNull(usageId, "优惠券使用记录ID不能为空");
        CouponUsage couponUsage = couponUsageMapper.selectById(usageId);
        AssertUtil.isNotNull(couponUsage, "优惠券使用记录不存在");
        couponUsage.setStatus(BaseConstants.LOSE_STATUS);
        couponUsage.setUpdateDate(new Date());
        couponUsageMapper.updateById(couponUsage);
    }

    @Override
    public void deleteBatch(List<Long> usageIds) {
        AssertUtil.isNotNull(usageIds, "优惠券使用记录ID列表不能为空");
        List<CouponUsage> couponUsages = couponUsageMapper.selectBatchIds(usageIds);
        AssertUtil.isNotNull(couponUsages, "优惠券使用记录不存在");
        for (CouponUsage couponUsage : couponUsages) {
            couponUsage.setStatus(BaseConstants.LOSE_STATUS);
            couponUsage.setUpdateDate(new Date());
            couponUsageMapper.updateById(couponUsage);
        }
    }
}
